package com.cxm.qyyz.app;

import android.app.Application;
import android.content.Context;
import android.media.MediaPlayer;
import android.text.TextUtils;

import com.cxm.qyyz.di.component.AppComponent;
import com.cxm.qyyz.di.component.DaggerAppComponent;
import com.cxm.qyyz.di.module.AppModule;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class App extends Application {

    private static App instance;
    private AppComponent appComponent;

    static {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.finishRefresh(1000);
            return new ClassicsHeader(context);
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //初始化AppComponent
        initAppComponent();
    }

    //初始化  热云  umeng  还有推送
    public void initSDK() {
    }



    public static App getInstance() {
        return instance;
    }

    public void initAppComponent() {
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }


    public AppComponent getAppComponent() {
        return appComponent;
    }

    // 如果返回值为null，则全部使用默认参数。

    public static boolean inMainProcess(Context context) {
        String mainProcessName = context.getApplicationInfo().processName;
        String processName = getProcessName();
        return TextUtils.equals(mainProcessName, processName);
    }


    public MediaPlayer mediaPlayer;


    /**
     * 获取当前进程名
     */
    public static String getProcessName() {
        BufferedReader reader = null;
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            reader = new BufferedReader(new FileReader(file));
            return reader.readLine().trim();
        } catch (IOException e) {
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
